<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>vertical-gallery</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        .gallery-container {
            display: flex;
            justify-content: center;
        }
        .thumbnails {
            display: flex;
            flex-direction: column;
            gap: 8px;
        }
        .thumbnails > .thumbnails-img {
            width: 40px;
            height: 40px;
            cursor: pointer;
        }
        .scrollbar {
            width: 1px;
            height: 720px;
            background-color: #cecece;
            display: block;
            margin: 0 0 0 8px;
        }
        .thumb {
            width: 1px;
            background-color: #2396ef;
            height: 0;
            position: absolute;
        }
        .slides {
            margin: 0 16px;
            display: grid;
            grid-auto-flow: row;
            gap: 1rem;
            width: calc(540px + 1rem);
            padding: 0 .25rem;
            height: 720px;
            overflow-y: auto;
            overscroll-behavior-y: contain;
            scroll-snap-type: y mandatory;
            scrollbar-width: none;
        }
        .slides-item {
            scroll-snap-align: start;
        }
        .slides-item > img {
            width: 540px;
            object-fit: contain;
        }
        .slides::-webkit-scrollbar {
            display: none;
        }
    </style>
</head>
<body>
    <div class="gallery-container">
        <div class="thumbnails"></div>
        <div class="scrollbar">
            <div class="thumb"></div>
        </div>
        <div class="slides">
            <div class="slides-item"><img src="https://www.eveningwater.com/img/segmentfault/1.png" alt="图片加载中"></div>
            <div class="slides-item"><img src="https://www.eveningwater.com/img/segmentfault/2.png" alt="图片加载中"></div>
            <div class="slides-item"><img src="https://www.eveningwater.com/img/segmentfault/3.png" alt="图片加载中"></div>
            <div class="slides-item"><img src="https://www.eveningwater.com/img/segmentfault/4.png" alt="图片加载中"></div>
            <div class="slides-item"><img src="https://www.eveningwater.com/img/segmentfault/5.png" alt="图片加载中"></div>
            <div class="slides-item"><img src="https://www.eveningwater.com/img/segmentfault/6.png" alt="图片加载中"></div>
            <div class="slides-item"><img src="https://www.eveningwater.com/img/segmentfault/7.png" alt="图片加载中"></div>
            <div class="slides-item"><img src="https://www.eveningwater.com/img/segmentfault/8.png" alt="图片加载中"></div>
            <div class="slides-item"><img src="https://www.eveningwater.com/img/segmentfault/9.png" alt="图片加载中"></div>
        </div>
    </div>
    <script>
        const slideGallery = document.querySelector('.slides'),
              slides = slideGallery.querySelectorAll('.slides-item'),
              scrollbarThumb = document.querySelector('.thumb'),
              slideCount = slides.length,
              slideHeight = 720,
              marginTop = 16;
        const scrollThumb = () => {
            const index = Math.floor(slideGallery.scrollTop / slideHeight);
            scrollbarThumb.style.height = `${((index + 1) / slideCount) * slideHeight}px`
        }
        const scrollToElement = el => {
            const index = parseInt(el.dataset.id,10);
            slideGallery.scrollTo(0,index * slideHeight + marginTop);
        }
        document.querySelector('.thumbnails').innerHTML += [...slides].map((slide,i) => `<img src="${slide.querySelector('img').src}" data-id="${i}" class="thumbnails-img" />`).join('');
        slideGallery.addEventListener('scroll',e => scrollThumb());
    </script>
</body>
</html>